import type { Dayjs } from "dayjs";
import dayjs from "dayjs";
import { Value } from "./type";

export const dateFormat = (date: Dayjs | null) => date?.format("YYYY-MM-DD");
export const timeRangeFormat = (
  date: number | null,
  disabledTime?: boolean
) => {
  if (date) {
    const formatStr = disabledTime ? "YYYY-MM-DD" : "YYYY-MM-DD HH:mm:ss";
    return dayjs(date)?.format(formatStr);
  }
  return undefined;
};

export const getInitStateValue = (
  value: Value | null
): [number | null, number | null] => {
  if (!value) {
    return [null, null];
  }
  const isValid =
    !!value &&
    value.length === 2 &&
    !!value[0] &&
    !!value[1] &&
    dayjs(value[0]).isSameOrAfter(value[1]);
  if (isValid) {
    return [value[1], value[0]];
  }
  return value;
};

export const enMonthMapping = new Map([
  [1, "Jan"],
  [2, "Feb"],
  [3, "Mar"],
  [4, "Apr"],
  [5, "May"],
  [6, "Jun"],
  [7, "Jul"],
  [8, "Aug"],
  [9, "Sep"],
  [10, "Oct"],
  [11, "Nov"],
  [12, "Dec"],
]);
